<?php
/**
 * Created by PhpStorm.
 * User: wangan
 * Date: 2018/3/28
 * Time: 10:11
 */
namespace App\Service;

use App\Model\SalesDetail;
use Illuminate\Support\Facades\DB;

class SaleDetailService
{

    /**
     * @desc 根据时间段获取 统计信息
    */
    public static function getCountInfoBySaleTime($int_beginTime, $int_endTime){
        $model_res = SalesDetail::whereBetween('sale_time', [$int_beginTime, $int_endTime])
            ->select([
                DB::raw('count(*) as count'),
                DB::raw('coalesce(sum(profit),0) as profit_sum'),
                DB::raw('coalesce(sum(sale_price),0) as sale_price_sum'),
            ])->first();
        return $model_res;
    }

    /*
     *  * @desc 根据时间段获取 统计每天销售信息
     * */
    public static function getCountInfoGroupBySaleTime($int_beginTime, $int_endTime){
        $model_res = SalesDetail::whereBetween('sale_time', [$int_beginTime, $int_endTime])
            ->select([
                DB::raw('from_unixtime(sale_time,"%Y-%m-%d") as sale_date'),
                DB::raw('count(*) as count'),
                DB::raw('coalesce(sum(profit),0) as profit_sum'),
                DB::raw('coalesce(sum(sale_price),0) as sale_price_sum'),
            ])
            ->groupBy('sale_date')
            ->get();

        return $model_res;
    }
}